package C_Paper;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class C100_10 {
    //开源热度
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] qz = new int[5];
        for (int i = 0; i < 5; i++) {
            qz[i] = sc.nextInt();
        }
        List<String[]> t = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String[] x = new String[7];
            x[0] = sc.next();
            int sum = 0;
            for (int j = 1; j < 6; j++) {
                int m = sc.nextInt();
                sum += (m * qz[j-1]);
            }
            x[6] = String.valueOf(sum);
            t.add(x);
        }
        t.sort(Comparator.comparing((String[] m) -> Integer.parseInt(m[6])).reversed().thenComparing((String[] m) -> m[0].toLowerCase()));
        for (String[] strings : t) {
            System.out.println(strings[0]);
        }
    }
}
